package com.gxuwz.endrskyweather.adapter;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import com.gxuwz.endrskyweather.WeatherPagerFragment;
import com.gxuwz.endrskyweather.model.City;

import java.util.ArrayList;
import java.util.List;

public class WeatherPagerAdapter extends FragmentStateAdapter {
    private List<String> cities = new ArrayList<>();
    private List<String> citiesCode = new ArrayList<>();

    public WeatherPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    public WeatherPagerAdapter(@NonNull Fragment fragment) {
        super(fragment);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return WeatherPagerFragment.newInstance(cities.get(position), citiesCode.get(position));
    }

    @Override
    public int getItemCount() {
        return cities.size();
    }

    public void setCities(List<City> objList) {
        for (City city : objList) {
            cities.add(city.getAreaCn());
            citiesCode.add(city.getAreaCode());
        }
        Log.d("WeatherPagerAdapter", "Cities updated: " + cities.size() + " items");
        notifyDataSetChanged();
    }

    public List<String> getCities() {
        return cities;
    }
}
